package com.zm.shop.listener;

import com.rabbitmq.client.Channel;
import com.zm.shop.common.constants.RabbitMQCons;
import com.zm.shop.service.Impl.GoodsServiceImpl;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import java.io.IOException;


@Component  //将获取的商品信息插入到ES里
public class GoodsQueueListener {

    @Autowired
    private GoodsServiceImpl goodsServiceImpl;

    @RabbitListener(queues = RabbitMQCons.GOODS_ESQUEUE)
    public void saveGoodsToEs(String goodsEntityJSON, Channel channel, Message message){


        try {
            //插入到ES
            goodsServiceImpl.addGoods(goodsEntityJSON);
            // 手动ack
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
